package com.gpnu.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpnu.crm.domain.Employee;
import com.gpnu.crm.mapper.EmployeeMapper;
import com.gpnu.crm.mapper.PermissionMapper;
import com.gpnu.crm.mapper.RoleMapper;
import com.gpnu.crm.query.EmployeeQueryObject;
import com.gpnu.crm.query.QueryObject;
import com.gpnu.crm.service.IEmployeeService;

//员工对象在业务层实现，需要交给Spring来创建对象
@Service
public class EmployeeServiceImpl implements IEmployeeService {

	// 拿到mapper接口的代理对象
	@Autowired // 告诉Spring此从容器中找到对应的对象，自动赋值过来
	private EmployeeMapper employeeMapper;
	@Autowired
	private PermissionMapper permissionMapper;
	@Autowired
	private RoleMapper roleMapper;

	public void saveOrUpdate(Employee entity) {
		// 如果页面传递过来的参数中id为null，说明是保存的请求
		// 反之，是更新的请求
		if (entity.getId() != null) {
			// 更新
			employeeMapper.update(entity);
		} else {
			// 保存
			employeeMapper.insert(entity);
		}

	}

	public void delete(Long id) {
		employeeMapper.delete(id);
	}

	public Employee get(Long id) {
		return employeeMapper.selectById(id);
	}

	public PageInfo<Employee> list(EmployeeQueryObject qo) {
		// 告诉插件，下一个查询需要分页
		// PageHelper.startPage (查询哪页在数据， 查询多少行)
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<Employee> list = employeeMapper.selectAll(qo);
		return new PageInfo<>(list);
	}

	public List<Employee> queryNewSellers(int id) {
		return employeeMapper.queryNewSellers(id);
	}

	public List<String> getRoleNamesByEmployeeId(Long id) {
		return this.roleMapper.selectRoleNamesByEmployeeId(id);
	}

	public List<String> getPermissionsByEmployeeId(Long id) {
		return this.permissionMapper.selectByEmployeeId(id);
	}

	public Employee getByUsername(String username) {
		return this.employeeMapper.selectByInfo(username);
	}

	@Override
	public void batchDelete(Long[] ids) {
		// TODO Auto-generated method stub	
		this.employeeMapper.batchDelete(ids);		
	}

	@Override
	public List<Employee> list() {
		// TODO Auto-generated method stub
		///////////////
		return this.employeeMapper.list();
	}

}
